#!/usr/bin/bash

app_install() {
	case $1 in
		(vscode)
			yay -S visual-studio-code-bin
			;;

		(obsidian)
			sudo pacman -S obsidian
			;;

		(miniconda3)
			if [ -f /opt/miniconda3 ]; then
				echo "Miniconda3 has beed installed !"
			else
				echo "You can press q to quit the license"
				echo "The recommended location to install miniconda3 is /opt/miniconda3"

				if [ -e Miniconda3-latest-Linux-x86_64.sh ] || [ -e miniconda3.sh ]; then
					echo "sh file exist"
				else
					wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda3.sh
				fi

				sudo bash miniconda3.sh
			fi
			;;

		(todesk)
			wget https://newdl.todesk.com/linux/todesk_4.1.0_x86_64.pkg.tar.zst -O todesk.pkg.tar.zst
			sudo pacman -U todesk.pkg.tar.zst
			sudo systemctl start todeskd
			;;

		(qq)
			echo -e "\e[33mSuggest you to choose the nt version\e[0m"
			yay -S qq
			;;

		(nutstore)
			sudo pacman -S nutstore
			;;

		(clion)
			yay -S clion clion-jre
			;;

		(pycharm)
			pycharm_type=none
			while [ $pycharm_type != "Community" ] && [ $pycharm_type != "Professional" ]; do
				read -p "Community or Professional : " pycharm_type
			done

			if [ $pycharm_type == "Community" ]; then
				sudo pacman -S pycharm-community-edition
			else
				yay -S pycharm-professional
			fi
			;;

		(wudao-dict)
			yay -S wudao-dict-git
			;;
	esac
}


tool_list=(vscode obsidian miniconda3 todesk qq nutstore clion pycharm wudao-dict)
to_install=()
condition=none

echo "Welcome to tool installation !"
echo "You can use this script to install ${tool_list[*]}"

# choose the tools to be installed
for app in ${tool_list[*]}; do
	echo "Do you want to install $app ?"
	if [ $app == "miniconda3" ]; then
		echo -e "\e[33mSuggest you to install miniconda3 with 'miniconda_install.sh' !\e[0m"
	fi
	while [ $condition != "yes" ] && [ $condition != "no" ]; do
		read -p "(yes/no) " condition
	done
	if [ $condition == "yes" ]; then
		to_install+=($app)
	fi
	condition=none
done


echo "Clean the packages after installation ?"
read -p "(yes/no) " is_clean


echo "Now, we are going to install ${to_install[*]} ..."


count=0
for installapp in ${to_install[*]}; do
	echo -e "\e[36mStep $count : Installing $installapp ...\e[0m"
	count=$(($count + 1))
	app_install $installapp
	echo -e "\e[32mSuccessfully install $installapp !\e[0m"
done


if [ $is_clean == "yes" ]; then
	echo "Cleaning the packages ..."
	for cleanapp in ${to_install[*]}; do
		rm $cleanapp*
	done
	yay -Sc
fi


echo -e "\e[32mSucceed to install ${#to_install[@]} packages !\e[0m"
echo "Have a good experience with these tools !"
